package com.wh.wisdomsite.xbox.ba.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.ba.service.IXboxLogService;
import com.wh.wisdomsite.xbox.ba.dao.XboxLogMapper;
import com.wh.wisdomsite.xbox.ba.dao.XboxSubLogMapper;
import com.wh.wisdomsite.xbox.ba.entity.XboxSubLog;
import com.wh.wisdomsite.xbox.ba.dao.XboxIpLocationMapper;
import com.wh.wisdomsite.xbox.ba.entity.XboxLog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * xbox日志ServiceImpl
 * @Package com.wh.wisdomsite.xbox.ba.service.impl
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
@Service("xboxLogService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class XboxLogServiceImpl extends ServiceImpl<XboxLogMapper, XboxLog> implements IXboxLogService {


    private static final Log logger = LogFactory.getLog(XboxLogServiceImpl.class);

    @Autowired
    private XboxLogMapper xboxLogMapper;

    @Autowired
    private XboxSubLogMapper xboxSubLogMapper;

    @Autowired
    private XboxIpLocationMapper xboxIpLocationMapper;


    @Override
    public XboxLog getById(String id) {
        XboxLog xboxLog = xboxLogMapper.selectById(id);
        if (null != xboxLog) {
            XboxSubLog oneKey = new XboxSubLog();
            oneKey.setRid(xboxLog.getId());
            XboxSubLog xboxSubLog = xboxSubLogMapper.selectOne(oneKey);
            if (null != xboxSubLog) {
                xboxLog.setBindObject(xboxSubLog.getBindObject());
                xboxLog.setOutput(xboxSubLog.getOutput());
                xboxLog.setException(xboxSubLog.getException());
            }
        }
        return xboxLog;
    }

    @Override
    public void save(XboxLog xboxLog) {
        try{
            //添加主日志
            xboxLogMapper.insert(xboxLog);
            XboxSubLog xboxSubLog = new XboxSubLog();
            xboxSubLog.setRid(xboxLog.getId());
            xboxSubLog.setBindObject(xboxLog.getBindObject());
            xboxSubLog.setOutput(xboxLog.getOutput());
            xboxSubLog.setException(xboxLog.getException());
            xboxSubLog.setCreateDate(xboxLog.getCreateDate());
            xboxSubLog.setUpdateDate(xboxLog.getUpdateDate());
            //添加子日志
            xboxSubLogMapper.insert(xboxSubLog);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("添加xbox日志记录Exception: " + e.getMessage());
            throw new RuntimeException("添加xbox日志异常");
        }
    }



}
